package com.jiugang.cardump.controller;


import com.alibaba.fastjson.JSONObject;
import com.jiugang.cardump.entity.ProEquRef;
import com.jiugang.cardump.pojo.CarDumperPOJO;
import com.jiugang.cardump.pojo.MonitorParamPOJO;
import com.jiugang.cardump.service.MonitorService;
import com.jiugang.cardump.util.IdempotentUtils;
import com.jiugang.cardump.util.TypeEnum;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.jiugang.cardump.bean.InformationBean;
import com.jiugang.cardump.constant.StatusCodeConstant;
import com.jiugang.cardump.exception.SystemExceptionV110;
import com.jiugang.cardump.result.ResultInfoV110;
import com.jiugang.cardump.service.MonitorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2021-08-19
 */
@RestController
@RequestMapping("/monitor")
@Slf4j
public class MonitorController {

    @Autowired
    private MonitorService monitorService;

    /**
     * @param equipmentId
     * @returnResultInfoV110
     * @Description 根据设备Id查询最新10条设备车钩状态接口
     */
    @GetMapping("/hookStatu/list")
    public ResultInfoV110 queryEquipmentHookStatuList(String equipmentId){
        log.debug("MonitorController的queryEquipmentHookStatuList方法进入,参数equipmentId为"+equipmentId);
        //校验参数
        if (equipmentId == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "车钩状态信息"));
        }
        return monitorService.selectEuipmentHookStatuList(equipmentId);
    }

    /**
     * @param equipmentId
     * @returnResultInfoV110
     * @Description 根据设备Id查询最新1条设备压车臂状态接口
     */
    @GetMapping("/pressArm/list")
    public ResultInfoV110 queryEuipmentPressArmStuatuList(String equipmentId){
        log.debug("MonitorController的queryEuipmentPressArmStuatuList方法进入,参数equipmentId为"+equipmentId);
        //校验参数
        if(equipmentId == null){
            throw new SystemExceptionV110( InformationBean.setInformationBean(StatusCodeConstant.F002,"压车臂状态信息"));
        }
        return monitorService.selectEuipmentPressArmStatuList(equipmentId);
    }

    /**
     * @param monitorParamPOJO
     * @returnResultInfoV110
     * @Description 根据设备Id查询设备压车臂状态接口,分页
     */
    @GetMapping("/pressArmPage/list")
    public ResultInfoV110 queryEuipmentPressArmStuatuPageList(MonitorParamPOJO monitorParamPOJO){
        log.debug("MonitorController的queryEuipmentPressArmStuatuPageList方法进入,参数equipmentId为"+monitorParamPOJO);
        //校验参数
        if(monitorParamPOJO == null){
            throw new SystemExceptionV110( InformationBean.setInformationBean(StatusCodeConstant.F002,"压车臂状态信息"));
        }
        return monitorService.selectEuipmentPressArmStatuPageList(monitorParamPOJO);
    }

    /**
     * @param equipmentId
     * @returnResultInfoV110
     * @Description 根据设备Id查询最新10条设备轨道状态接口
     */
    @GetMapping("/trackStatu/list")
    public ResultInfoV110 queryEquipmentTrackStatuList(String equipmentId){
        log.debug("MonitorController的queryqueryEquipmentTrackStatuList方法进入,参数equipmentId为"+equipmentId);
        //校验参数
        if(equipmentId == null){
            throw new SystemExceptionV110( InformationBean.setInformationBean(StatusCodeConstant.F002,"轨道状态信息"));
        }
        return monitorService.selectEuipmentTrackStatuList(equipmentId);
    }

    /**
     * @param monitorParamPOJO
     * @returnResultInfoV110
     * @Description 根据设备Id查询设备轨道状态接口,分页
     */
    @GetMapping("/trackPage/list")
    public ResultInfoV110 queryEuipmentTrackStuatuPageList(MonitorParamPOJO monitorParamPOJO){
        log.debug("MonitorController的queryEuipmentTrackStuatuPageList方法进入,参数equipmentId为"+monitorParamPOJO);
        //校验参数
        if(monitorParamPOJO == null){
            throw new SystemExceptionV110( InformationBean.setInformationBean(StatusCodeConstant.F002,"压车臂状态信息"));
        }
        return monitorService.selectEuipmenTrackStatuPageList(monitorParamPOJO);
    }

    /**
     * @Author luoxin
     * @Description 监测数据接口
     * @Date 3:35 下午 2021/8/20
     * @param monitor
     * @return
     */
    @PostMapping
    public ResultInfoV110 add(@RequestBody String monitor) {
        log.info("MonitorController的add方法进入，参数为" + monitor);
        //判断请求是否重复
        if (!IdempotentUtils.judge(monitor, this.getClass())){
            throw  new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F004,"实时数据"));
        }
        //校验参数
        if (monitor == null) {
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002, "传入数据"));
        }
        //字符串转换Json对象
        JSONObject jsonObject = JSONObject.parseObject(monitor);
        //json对象转换baomingpojo对象
        CarDumperPOJO carDumperPOJO = JSONObject.toJavaObject(jsonObject, CarDumperPOJO.class);

        return monitorService.insert(carDumperPOJO);
    }

    /**
     * @Author luoxin
     * @Description 车钩状态查询接口
     * @Date 10:44 上午 2021/8/23
     * @param id,page,size,StartTime,endTime
     * @return
     */
    @GetMapping("/hookStatu/alllist")
    public ResultInfoV110 queryEquipmentAlertAllList(String id, Integer page, Integer size, String StartTime, String endTime){
        log.info("AlertController的queryEquipmentAlertList的方法进入,参数id为"+id);

        //参数校验
        if(id == null){
            throw new SystemExceptionV110(InformationBean.setInformationBean(StatusCodeConstant.F002,"报警查询信息"));
        }
        return  monitorService.selectEuipmentHookStatuAllList(id,page,size,StartTime,endTime);
    }

}

